﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using AngleSharp;
using AngleSharp.Dom;
using AngleSharp.Extensions;
using AngleSharp.Parser.Html;
using Common.Dao;
using Common.Model;
using ikvm.extensions;
using Spider.Service;

namespace Spider.Spider
{
    public class GszSpider
    {
        public static readonly string From = "云财经";
        public static readonly string ID_ = "ycj_";

        public static readonly string SiteBaseUrl = @"http://www.yuncaijing.com/tag/id_9.html";
        
        public static readonly string SectionClassName = @"main fl";
        public static readonly string TimeClassName = @"time";
        public static readonly string TitleClassName = @"title";
        public static readonly string TypeClassName = @"type";
        public static readonly string Href = @"href";
        public static readonly string AbsClassName = @"des";

        public List<News> GetNews(DateTime startTime,DateTime endTime)
        {
            var config = Configuration.Default.WithDefaultLoader();
            var address = SiteBaseUrl;

            var contentList = new List<News>();
            int i = 0;
            while (true)
            {
                i++;
                var url = SiteBaseUrl + "?page=" + i;
                var document = new HtmlParser().Parse(GetHtml(url));
                var cellSelector = "section";
                var sections = document.QuerySelectorAll(cellSelector);
                var mainFl = sections.Where(m => SectionClassName.Equals(m.ClassName));
                var section = mainFl.First();
//                var ul = section.QuerySelectorAll("ul").First();
                var querySelectorAll = section.QuerySelectorAll("li");
                List<News> contents = querySelectorAll.Select(SelectContentFromLi).ToList();

                if (contents.Any(content => content.SiteTime <= startTime))
                {
                    contentList.AddRange(contents.Where(content => content.SiteTime > startTime));
                    break;
                }
                contentList.AddRange(contents);
            }

            return contentList.Where(m => m.SiteTime < endTime).ToList();
        }
        
        private static HttpWebRequest GetRequest(string url)
        {
            HttpWebRequest request = (HttpWebRequest) WebRequest.Create(url);

            request.Method = WebRequestMethods.Http.Get;

            request.ContentType = "application/x-www-form-urlencoded; charset=UTF-8";
            request.Accept = "application/json, text/javascript, */*; q=0.01";
            request.Headers.Add("Origin:http://www.yuncaijing.com");
            request.Headers.Add("Pragma:no-cache");
//            request.Headers.Add("Referer:" + url);
            request.Headers.Add("X-Requested-With:XMLHttpRequest");
            request.UserAgent =
                "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36";

//            var cookieContainer = new CookieContainer(3);
//            request.CookieContainer = cookieContainer;

//            var cookie = new Cookie("web_uinfo", "%7B%22uactime%22%3A1515139618%7D");
//            request.CookieContainer.Add(new Uri("http://api.quchaogu.com/"), cookie);
//         
//            cookie = new Cookie("vistor_uuid", "51990107686080739545656");
//            request.CookieContainer.Add(new Uri("http://api.quchaogu.com/"), cookie);
//         
//            cookie = new Cookie("web_qtstr",
//                "7%253A3%253A1161120864%253Aa869763025534267%253A6%253A1515137301%253A22d4db999352996");
//            request.CookieContainer.Add(new Uri("http://api.quchaogu.com/"), cookie);
            return request;
        }
        
        /// <summary>
        /// </summary>
        private static string GetHtml(string url)
        {
            var httpWebRequest = GetRequest(url);

            HttpWebResponse response = (HttpWebResponse) httpWebRequest.GetResponse();
            Stream myResponseStream = response.GetResponseStream();
            StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
            string retString = myStreamReader.ReadToEnd();

            myStreamReader.Close();
            myResponseStream.Close();

            return retString;
        }
        
        private static News SelectContentFromLi(IElement element)
        {
            var time = element.QuerySelector(TimeClassName).TextContent;
            var titleDiv = element.QuerySelectorAll("a").Where(e => TitleClassName.Equals(e.ClassName)).ToList();
            var title = titleDiv.Select(e => e.GetAttribute(TitleClassName)).First();
            var href = titleDiv.Select(e => e.GetAttribute(Href)).First();
            var content = element.QuerySelectorAll("span").Where(e => AbsClassName.Equals(e.ClassName))
                .Select(m => m.TextContent).First();
            var id = element.GetAttribute("data-id");
            var typeDiv = element.QuerySelectorAll("a").Where(e => TypeClassName.Equals(e.ClassName)).ToList();

            var stock = "";
            var stockDiv = element.QuerySelectorAll("a").Where(e => e.HasAttribute("data-showchart-code")).ToList();

            var type = "";
            if (typeDiv.Count > 0)
            {
                type = typeDiv.Select(e => e.Text()).First();
            }

            if (stockDiv.Count > 0)
            {
                stock = stockDiv.First().GetAttribute("data-showchart-code");
            }
            else
            {
                char[] sp = { '：' };
                var strings = title.Split(sp, StringSplitOptions.RemoveEmptyEntries);
                if (strings.Length == 2)
                {
                    stock = StockCheckerService.GetStockCode(strings[0]);
                }
            }

            return new News
            {
                Type =  type,
                Title = title.Replace("云财经讯，","").Replace("预告：","").Replace("媒体：","").Replace("云财经App讯，", "").Replace("云财经APP讯,",""),
                SiteTime =DateTime.Parse(time),
                ContentLink = SiteBaseUrl + href,
                Abs = content.Replace("云财经讯，","").Replace("云财经App讯，", "").Replace("云财经APP讯,", ""),
                From = From,
                Tag = "",
                ContentId =  ID_+ id,
                Stocks = stock
            };
        }
    }
}
